package com.kws.merchant.mapper;

import com.kws.merchant.domain.po.Page;
import com.kws.merchant.domain.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/11/8 15:07
 * @Version: 1.0
 */
public interface UserPartnerMapper {

    @Select("<script>" +
            "SELECT " +
            " count(a.id) as `count` " +
            " FROM " +
            " tb_serve AS a " +
            " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id  " +
            " WHERE " +
            " a.`status` = 0" +
            " <if test='merchantName != null and merchantName!=\"\"'>  AND b.company_name like '%${merchantName}%'  </if>  " +
            " <if test='serveName != null and serveName!=\"\"'>  AND a.name like '%${serveName}%'  </if>  " +
            " <if test='type != null and type!=\"\"'>  AND a.type = '${type}'  </if>  " +
            " <if test='shopsId != null'>  AND a.merchant_id = #{shopsId}  </if>  " +
            " <if test='merchantType != null and merchantType != \"\"'>  AND b.user_role = '${merchantType}'  </if>  " +
            "</script>")
    Long userServeCount(@Param("serveName") String serveName,
                        @Param("merchantName") String merchantName,
                        @Param("type") String type,
                        @Param("shopsId") Long shopsId,
                        @Param("merchantType") String merchantType);


    @Select(
            "<script>" +
                    "SELECT " +
                    " a.id AS serve_id, " +
                    " a.`name`, " +
                    " a.type, " +
                    " a.price, " +
                    " a.price_tag, " +
                    " a.img, " +
                    " a.remark, " +
                    " b.company_name AS shops_name, " +
                    " b.id AS shops_id, " +
                    " b.company_address, " +
                    " b.user_role AS merchant_type, " +
                    " b.company_address_lat , " +
                    " b.company_address_lng , " +
                    " c .shop_score as score" +
                    " FROM " +
                    " tb_serve AS a " +
                    " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id " +
                    " LEFT JOIN tb_merchant_score AS c ON b.id = c.merchant_id  " +
                    " WHERE " +
                    " a.`status` = 0 " +
                    " <if test='serveName != null and serveName != \"\"'>  AND a.name like '%${serveName}%'  </if>  " +
                    " <if test='merchantName != null and merchantName != \"\"'>  AND b.company_name like '%${merchantName}%'  </if>  " +
                    " <if test='type != null and type != \"\"'>  AND a.type = '${type}'  </if>  " +
                    " <if test='shopsId != null'>  AND a.merchant_id = #{shopsId}  </if>  " +
                    " <if test='merchantType != null and merchantType != \"\"'>  AND b.user_role = '${merchantType}'  </if>  " +
                    " limit #{page},#{size} " +
                    "</script>")
    List<UserServeVo> userServeList(@Param("serveName") String serveName,
                                    @Param("merchantName") String merchantName,
                                    @Param("type") String type,
                                    @Param("shopsId") Long shopsId,
                                    @Param("merchantType") String merchantType,
                                    @Param("page") Integer page,
                                    @Param("size") Integer size);

    @Select("SELECT " +
            " a.id AS serve_id, " +
            " a.`name`, " +
            " a.type, " +
            " a.price, " +
            " a.price_tag, " +
            " a.img, " +
            " a.remark, " +
            " b.company_name AS shops_name, " +
            " b.id AS shops_id, " +
            " b.logo AS shops_logo, " +
            " b.company_address, " +
            " b.user_role AS merchant_type, " +
            " b.company_address_lat , " +
            " b.company_address_lng , " +
            " b.corporate_name , " +
            " b.registration_certificate_number , " +
            " c .shop_score as score" +
            " FROM " +
            " tb_serve AS a " +
            " LEFT JOIN tb_merchant AS b ON a.merchant_id = b.id " +
            " LEFT JOIN tb_merchant_score AS c ON b.id = c.merchant_id  " +
            " WHERE " +
            " a.`status` = 0 " +
            " and a.id = ${serveId} ")
    ServeInfoVo userServe(@Param("serveId") Long serveId);

    @Select("SELECT " +
            " a.scheme_id, " +
            " a.scheme_name AS scheme_name, " +
            " b.id as quotation_id, " +
            " b.merchant_id, " +
            " b.price, " +
            " b.status, " +
            " c.company_name AS merchant_name  " +
            " FROM " +
            " tb_item_user_scheme AS a " +
            " LEFT JOIN tb_item_scheme_merchant AS b ON a.scheme_id = b.scheme_id " +
            " LEFT JOIN tb_merchant AS c ON b.merchant_id = c.id  " +
            " WHERE " +
            " b.`status` != 2" +
            " AND a.user_id = ${userId} " +
            " limit #{page},#{size}")
    List<QuotationVo> getOfferManage(@Param("userId") Long userId,
                                     @Param("page") Integer page,
                                     @Param("size") Integer size);


    @Select("SELECT " +
            " count( a.scheme_id )  " +
            "FROM " +
            " tb_item_user_scheme AS a " +
            " LEFT JOIN tb_item_scheme_merchant AS b ON a.scheme_id = b.scheme_id  " +
            "WHERE " +
            " b.`status` != 2  " +
            " AND a.user_id = ${userId}")
    Long getOfferManageCount(@Param("userId") Long userId);

    @Select("SELECT " +
            " a.id AS serve_id, " +
            " a.img, " +
            " a.merchant_id, " +
            " a.NAME, " +
            " a.price, " +
            " a.merchant_id as shops_id, " +
            " a.remark  " +
            "FROM " +
            " `tb_serve` AS a " +
            " INNER JOIN ( " +
            " SELECT " +
            "  goods_id, " +
            "  COUNT(*) AS occurrence  " +
            " FROM " +
            "  tb_order  " +
            " WHERE " +
            "  type = 1  " +
            "  AND goods_id IS NOT NULL  " +
            "  AND goods_id != ''  " +
            " GROUP BY " +
            "  goods_id  " +
            " ORDER BY " +
            "  occurrence DESC  " +
            " LIMIT 20  " +
            " ) AS b ON a.id = b.goods_id" +
            " LIMIT 20")
    List<UserServeVo> getPopularServe();


    @Select("SELECT " +
            " a.id AS serve_id, " +
            " a.img, " +
            " a.merchant_id, " +
            " a.NAME, " +
            " a.price, " +
            " a.merchant_id as shops_id, " +
            " a.remark  " +
            " FROM " +
            " `tb_serve` AS a " +
            " INNER JOIN ( " +
            " SELECT " +
            "  goods_id, " +
            "  COUNT(*) AS occurrence  " +
            " FROM " +
            "  tb_goods_view  " +
            " WHERE " +
            "  type = 0  " +
            "  AND goods_id IS NOT NULL  " +
            "  AND goods_id != ''  " +
            " GROUP BY " +
            "  goods_id  " +
            " ORDER BY " +
            "  occurrence DESC  " +
            " LIMIT 20  " +
            " ) AS b ON a.id = b.goods_id " +
            " LIMIT 20")
    List<UserServeVo> getRecommendServe();

    @Select("SELECT " +
            " a.id AS merchant_id, " +
            " a.logo as shop_img, " +
            " a.company_name AS merchant_name  " +
            "FROM " +
            " `tb_merchant` AS a " +
            " INNER JOIN ( " +
            " SELECT " +
            "  merchant_id, " +
            "  COUNT(*) AS occurrence  " +
            " FROM " +
            "  tb_order  " +
            " WHERE " +
            "  type = 1  AND" +
            "  merchant_id IS NOT NULL  " +
            "  AND merchant_id != ''  " +
            " GROUP BY " +
            "  merchant_id  " +
            " ORDER BY " +
            "  occurrence DESC  " +
            "  LIMIT 20  " +
            " ) AS b ON a.id = b.merchant_id  " +
            " LIMIT 20")
    List<MerchantDetailVo> getVideoNumber();

}
